import 'package:animal/utils/screen_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///数量控制
class NumControlWidget extends StatefulWidget {
  late TextEditingController? controller;

  NumControlWidget({Key? key,this.controller}) : super(key: key);

  @override
  State<NumControlWidget> createState() => _NumControlWidgetState();
}

class _NumControlWidgetState extends State<NumControlWidget> {

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        InkWell(
          child: Container(
            width: ScreenUtil().L(50),
            height: ScreenUtil().L(30),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                border: Border.all(color: Colors.black,width: ScreenUtil().L(1))
            ),
            child: const Icon(Icons.add),
          ),
          onTap: ()=>numAdd(),
        ),
        Container(
          width: ScreenUtil().L(100),
          height: ScreenUtil().L(30),
          decoration: BoxDecoration(
            border: Border.symmetric(vertical: BorderSide(width: ScreenUtil().L(1),color: Colors.black))
          ),
          child: TextField(
            controller: widget.controller!,
            textAlign: TextAlign.center,
            decoration: const InputDecoration(
              border: InputBorder.none,
              isCollapsed: true,
            ),
            keyboardType: TextInputType.number,
            inputFormatters: [FilteringTextInputFormatter.digitsOnly],//只允许输入数字
          ),
        ),
        InkWell(
          child: Container(
            width: ScreenUtil().L(50),
            height: ScreenUtil().L(30),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.black,width: ScreenUtil().L(1))
            ),
            child: Image.asset("icons/icon_reduce.png"),
          ),
          onTap: ()=>numReduce(),
        ),
      ],
    );
  }

  ///数量增加的方法
  void numAdd(){
    int a = int.parse(widget.controller!.text.trim());
    setState(() {
      ++a;
    });
  }

  ///数量增加的方法
  void numReduce(){
    int a = int.parse(widget.controller!.text.trim());
    setState(() {
      --a;
    });
  }
}
